package cn.fsw.pethome.org.controller;

import cn.fsw.pethome.org.domain.Employee;
import cn.fsw.pethome.org.query.EmployeeQuery;
import cn.fsw.pethome.org.service.IEmployeeService;
import cn.fsw.pethome.org.util.AjaxResult;
import cn.fsw.pethome.org.util.PageList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @GetMapping("/list")
    @ApiOperation(value = "查询全部员工信息")
    public List<Employee> selectAll() {
        return employeeService.selectAll();
    }

    @PostMapping("/pageList")
    @ApiOperation(value = "高级查询和分页")
    public PageList<Employee> pageSelevtAll(@RequestBody EmployeeQuery query) {

        return employeeService.pageSelevtAll(query);
    }

    @GetMapping("/list/{id}")
    @ApiOperation(value = "通过ID查询")
    public Employee selectById(@PathVariable("id") Long id) {
        return null;
    }


    @PutMapping("/employee")
    @ApiOperation(value = "新增或者修改员工信息")
    public AjaxResult update(@RequestBody Employee employee) {
        try {
            if (employee.getId() == null) {
                employeeService.add(employee);
            } else {
                employeeService.update(employee);
            }
            return AjaxResult.ajaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @DeleteMapping("/department/{id}")
    @ApiOperation(value = "通过ID删除" )
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return AjaxResult.ajaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
}
